﻿using DataAccess.DTO;
using DataAccess.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYD.DataAccess;
using XYD.Framework.Data;
using XYD.Framework.Data.Dapper;

namespace DataAccess.DAL
{
    public class AgencyUsersDAL : BaseDAL
    {
        public AgencyUsers Get(int id)
        {
            return CONN_TopicSharing.Get<AgencyUsers>(id);
        }

        public bool Insert(AgencyUsers user)
        {
            return CONN_TopicSharing.Insert<AgencyUsers>(user);
        }

        public bool Update(AgencyUsers user)
        {
            return CONN_TopicSharing.Update<AgencyUsers>(user);
        }

        public bool Exist(string username, string promoter_id)
        {
            return CONN_TopicSharing.SQLQuery<int>(" select count(1) from AgencyUsers where is_delete=0  and username=@username and promoter_id=@promoter_id ", new { username = username, promoter_id = promoter_id }).FirstOrDefault() > 0;
        }

        public bool ExistGroup(string group_name,int not_user_id)
        {
            return CONN_TopicSharing.SQLQuery<int>(string.Format(" select count(1) from AgencyUsers where is_delete=0  and group_name='{0}' and id!={1} ", group_name, not_user_id), null).FirstOrDefault() > 0;
        }


        public string GetSitePid(string username, string promoter_id)
        {
            return CONN_TopicSharing.SQLQuery<string>("  select SITE_PID from AgencyUsers where username=@username and promoter_id=@promoter_id and is_delete=0 and state=1 ", new { username = username, promoter_id = promoter_id }).FirstOrDefault();
        }

        public AgencyUsers Get(string username,int promoter_id)
        {
            string sql = " select * from AgencyUsers where username=@username and promoter_id=@promoter_id ";

            AgencyUsers user = CONN_TopicSharing.Get<AgencyUsers>(sql, new { username = username, promoter_id = promoter_id });

            return user;
        }


        public AgencyUsers Get(string username, string promoter_id)
        {
            string sql = " select top 1 * from AgencyUsers where username=@username and promoter_id=@promoter_id ";

            AgencyUsers user = CONN_TopicSharing.Get<AgencyUsers>(sql, new { username = username, promoter_id = promoter_id });

            return user;
        }

        public bool Login(string username, string password)
        {
            string sql = string.Format(" select * from AgencyUsers where username='{0}' and password='{1}' ", username, password);

            AgencyUsers user = CONN_TopicSharing.Get<AgencyUsers>(sql);
            if (user != null && user.state == 1)
            {
                return true;
            }

            return false;
        }

        public UserSession LoginSession(string username, string password, int promoter_id)
        {

            string sql = string.Format(" select top 1 id,username,nickname,state,promoter_id from AgencyUsers where username=@username and password=@password and  promoter_id=@promoter_id");

            AgencyUsers user = CONN_TopicSharing.Get<AgencyUsers>(sql, new  { username = username, password = XYD.Common.EncodeSecurity.EncryptString(password), promoter_id = promoter_id });
            if (user != null && user.state == 1)
            {
                UserSession userSession = new UserSession() { id = Guid.NewGuid().ToString("N"), user_id = user.id, user_name = user.username, nick_name = user.nickname, time_stamp = XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now), promoter_id=user.promoter_id, user_img=user.head_img };
                return userSession;
            }

            return null;
        }

        public UserSession GetLoginSession(string sessionid)
        {
            UserSession session = RedisOperation.GetUserSession(sessionid);
            return session;
        }

        public bool SetLoginSession(UserSession session)
        {
            return RedisOperation.SetUserSession(session);
        }

        public List<AgencyUsers> GetUsers(int promoter_id)
        {
            List<AgencyUsers> users = new List<AgencyUsers>();

            users = CONN_TopicSharing.SQLQuery<AgencyUsers>(" select * from AgencyUsers where is_delete=0 and state=1 and promoter_id=@promoter_id ", new { promoter_id = promoter_id }).ToList();

            return users;
        }

        public List<AgencyUsers> ListPage(string strWhere, string orderbys, int pagesize, int pageindex,object param, out int count)
        {
            List<AgencyUsers> users = new List<AgencyUsers>();
            if (string.IsNullOrEmpty(strWhere))
            {
                strWhere = " is_delete=0";
            }
           // strWhere += " and id!=1 ";  
            if (string.IsNullOrEmpty(orderbys))
                orderbys = "id";

            users = CONN_TopicSharing.SQLPager<AgencyUsers>("*", " AgencyUsers", strWhere, "", "", orderbys, pagesize, pageindex, out count,param).ToList();

            return users;
        }

        public List<AgencyUsers> GetUsersByGroupName(List<string> groupnames)
        {
            if(groupnames==null || !groupnames.Any())
            {
                return  new List<AgencyUsers>();
            }

            List<AgencyUsers> users = new List<AgencyUsers>();

            users = CONN_TopicSharing.SQLQuery<AgencyUsers>(string.Format(" select * from AgencyUsers where promotion=1 and group_name is not null and group_name !='' and state=1 and group_name in ('{0}') ", string.Join("','", groupnames)), null).ToList();

            return users;
        }

       
        /// <summary>
        /// 获取推广者的所有代理
        /// </summary>
        /// <param name="promoter_id"></param>
        /// <returns></returns>
        public List<AgencyUsers> GetListBy_Promoter_Id(int promoter_id)
        {
            if (promoter_id<=0)
            {
                return new List<AgencyUsers>();
            }

            List<AgencyUsers> users = new List<AgencyUsers>();

            users = CONN_TopicSharing.SQLQuery<AgencyUsers>(" select * from AgencyUsers where promoter_id=@promoter_id ", new { promoter_id = promoter_id }).ToList();

            return users;

        }



        
    }
}
